package com.hb.controller.leave;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.hb.model.Result;
import com.hb.pojo.dto.leave.LeaveDto;
import com.hb.pojo.po.leave.Leave;
import com.hb.pojo.po.role.Role;
import com.hb.pojo.vo.leave.LeaveVo;
import com.hb.pojo.vo.validated.GroupA;
import com.hb.pojo.vo.validated.GroupB;
import com.hb.pojo.vo.validated.GroupD;
import com.hb.service.leave.LeaveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.shiro.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;


@Api(tags = "Leave management Related interfaces")
@Transactional
@Validated
@RestController
@RequestMapping("leave")
public class LeaveController {

	@Resource
	private LeaveService leaveService;

	/**
	 * Ask for leave
	 */
	@ApiOperation(value = "Add a leave", notes = "Add a leave")
	@PostMapping("")
	public Result<Leave> insertSelectiveOne(@RequestBody(required = false) @Parameter(name = "add") @Validated(GroupA.class) LeaveDto leaveDto) {
		leaveDto.setCreate_time(new Date());
		leaveDto.setLastmodify(new Date());
		leaveDto.setRole("employee");

		Leave leave = leaveService.insertSelective(leaveDto);
		return leave != null ? Result.success(leave) : Result.error();
	}

	/**
	 * update a leave
	 */
	@ApiIgnore
	@ApiOperation(value = "update a leave", notes = "update a leave")
	@PatchMapping()
	public Result<Leave> updateSelectiveOne(@RequestBody @Parameter(name = "修改对象") @Validated(GroupB.class) LeaveVo leaveVo) {
		Leave leave = leaveService.updateByPrimaryKeySelective(leaveVo);
		return leave != null ? Result.success(leave) : Result.error();
	}

	/**
	 * query get leave list
	 */
	@ApiIgnore
	@ApiOperation(value = "query get leave list", notes = "query get leave list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNumber", value = "pages count", paramType = "query", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "pageSize", value = "record per page", paramType = "query", required = true, dataType = "String", dataTypeClass = String.class),
	})
	@GetMapping()
	public Result<LeaveDto> getList(@ApiIgnore @Validated(GroupD.class) LeaveVo leaveVo) throws ParseException {
		PageMethod.startPage(leaveVo.getPageNumber(), leaveVo.getPageSize());
		List<LeaveDto> leaveDtos = leaveService.selectByVo(leaveVo);

		Role role = (Role)  SecurityUtils.getSubject().getPrincipal();
		String username=role.getRole();
		for(LeaveDto leaveDto : leaveDtos){

			if(leaveDto.getStatus()==2){
				leaveDto.setApproveBtnVisable(0);
			}
			else if(leaveDto.getStatus()==1){
				if(username.equals("senior manager")){
					leaveDto.setApproveBtnVisable(1);
				}else{
					leaveDto.setApproveBtnVisable(0);
				}
			}else if(leaveDto.getStatus()==0){
				if(username.equals("manager")){
					leaveDto.setApproveBtnVisable(1);
				}else{
					leaveDto.setApproveBtnVisable(0);
				}
			}
			else {
				leaveDto.setApproveBtnVisable(0);
			}
		}

		PageInfo<LeaveDto> pageInfo = new PageInfo<>(leaveDtos);
		return Result.success(pageInfo);
	}



}
